﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime;
using System.Data;
using System.IO.Ports;

namespace C_DOS
{
    class Program
    {
        static void Main(string[] args)
        {
        boot://begin "boot" sequence:
            Console.WriteLine("Welcome to C-DOS");
            Console.WriteLine("Booting the system.....");
            Console.WriteLine("Loading Main System files.....");
            Console.WriteLine("Done.");
            Console.Clear();
        begin://main loop start
            string DateAndTime = DateTime.Today.ToString();
            Console.WriteLine(DateAndTime);
            Console.Write("\\root:");
            string command = Console.ReadLine();
            if (command == "help")//help menu command
            {
                Console.WriteLine("==============Help==============");
                Console.WriteLine("| help = help menu(obviously)  |");
                Console.WriteLine("| dir = directory of HardDrive |");
                Console.WriteLine("| edit = C-DOS TextWriter      |");
                Console.WriteLine("| ver = C-DOS Version          |");
                Console.WriteLine("| restart = restart C-DOS      |");
                Console.WriteLine("| shutdown = shutdown C-DOS    |");
                Console.WriteLine("| clr = clear the screen       |");
                Console.WriteLine("| run = run a .dat file(in c#) |");
                Console.WriteLine("================================");
                goto begin;
            }
            else
                if (command == "dir")//dir command
                {
                    string dir = Directory.GetCurrentDirectory();
                    Console.WriteLine(dir);
                    goto begin;
                }
                else
                    if (command == "edit")//edit command
                    {
                        Console.WriteLine("Welcome to DOSWriter");
                        Console.WriteLine("Version 1.0.0");
                    dosedit://doswriter main loop
                        Console.Write("\\DOSWriter:");
                        string editcom = Console.ReadLine();
                        if (editcom == "help")//doswriter help command
                        {
                            Console.WriteLine("==========DOSWriterHelp==========");
                            Console.WriteLine("| help = DOSWriter Help Menu    |");
                            Console.WriteLine("| new = new text file           |");
                            Console.WriteLine("| open = open a text file       |");
                            Console.WriteLine("| exit = exit the writer        |");
                            Console.WriteLine("| clr = Clear the screen        |");
                            Console.WriteLine("|                               |");
                            Console.WriteLine("| Note: Files written in this   |");
                            Console.WriteLine("| writer will be saved in the   |");
                            Console.WriteLine("| '.dat' format.                |");
                            Console.WriteLine("=================================");
                            goto dosedit;
                        }
                        else
                            if (editcom == "new")//doswriter create a new file command
                            {
                                Console.Write(">");
                                string docslot = Console.ReadLine();
                            beginsave://the save file "dialog" mini-loop
                                Console.WriteLine("Do you wish to save this Document?");
                                Console.Write("[y] or [n]");
                                string savedialog = Console.ReadLine();
                                if (savedialog == "y")//say yes to saving the document you just typed
                                {
                                    Console.WriteLine("Note: Document will be saved in current working directory.");
                                    Console.Write("Name for Document:");//give it a name
                                    string documentname = Console.ReadLine();
                                    string documentsavedirectory = Directory.GetCurrentDirectory();
                                    BinaryWriter documentwriter = new BinaryWriter(File.Create(documentsavedirectory + documentname));//create the document with the name provided in the working directory
                                    documentwriter.Write(docslot);//write the contents typed
                                    goto dosedit;
                                }
                                else
                                    if (savedialog == "n")//say no to save, get rid of the hard work of typing
                                    {
                                        Console.WriteLine("Ok, document not saved.");
                                        goto dosedit;
                                    }
                                    else
                                    {
                                        Console.WriteLine("ERROR: Not a correct answer.");//oh no, an error
                                        goto beginsave;
                                    }
                            }
                            else
                                if (editcom == "open")//lets open a previously made file
                                {
                                OpenDocLoop:
                                    Console.Write("Enter the Directory:");//enter the directory of the file
                                    string docdirectory = Console.ReadLine();
                                    if (docdirectory == "exit")
                                    {
                                        Console.WriteLine("Document not opened");
                                        goto dosedit;
                                    }
                                    else
                                        try
                                        {
                                            BinaryReader documentreader = new BinaryReader(File.Open(docdirectory, FileMode.Open));
                                            string thedocument = documentreader.ReadString();//open the file and read the contents
                                            Console.WriteLine(thedocument);
                                        }
                                        catch
                                        {
                                            Console.WriteLine("ERROR: Directory not found!!!");
                                            goto OpenDocLoop;
                                        }
                                    goto dosedit;
                                }
                                else
                                    if (editcom == "clr")//clear the screen(for the doswriter loop)
                                    {
                                        Console.Clear();
                                        goto dosedit;
                                    }
                                    else
                                        if (editcom == "exit")//exit the doswriter "program"
                                        {
                                            goto begin;
                                        }
                                        else
                                            if (editcom == "")//if you like to type random stuff, this prohibits errors
                                            {
                                                goto dosedit;
                                            }
                    }
                    else
                        if (command == "ver")//dos version command
                        {
                            string dosversion = ("C-DOS Version: 1.0.0");
                            Console.WriteLine(dosversion);
                            goto begin;
                        }
                        else
                            if (command == "restart")//restart the virtual dos
                            {
                                Console.WriteLine("Restarting.....");
                                Console.WriteLine("Ending Processes.....");
                                Console.WriteLine("Rebooting");
                                Console.Beep();
                                goto boot;
                            }
                            else
                                if (command == "shutdown")//turn off the program
                                {
                                    Console.WriteLine("Shutting down.....");
                                    Console.WriteLine("Ending Processes.....");
                                    Console.WriteLine("Closing.....");
                                    Console.Beep();
                                }
                                else
                                    if (command == "clr")//clear the screen(main loop)
                                    {
                                        Console.WriteLine("Clearing Screen");
                                        Console.Clear();
                                        goto begin;
                                    }
                                    else
                                        if (command == "run")//run an external file
                                        {
                                            Console.WriteLine("Enter Directory:");
                                            string runfile = Console.ReadLine();
                                            BinaryReader executefile = new BinaryReader(File.Open(runfile, FileMode.Open));
                                            string runexefile = executefile.ReadString();
                                            runexefile.GetTypeCode();
                                        }
        }
    }
}

